(function($) {
	$.fn.chart = function(ops) {
		var self = this;
		var o = $.extend({}, $.fn.chart.ops, ops);
		var paper = Raphael(self.selector, o.sx * (o.nx + 1) + o.l + o.r, o.sy * (o.ny + 1) + o.t + o.b);
		var pathArr = ["M", o.l + 0.5, o.t + 0.5, "L", o.sx * o.nx + o.l + 0.5, o.t + 0.5, "L", o.sx * o.nx + o.l + 0.5, o.sy * o.ny + o.t + 0.5, "L", o.l + 0.5, o.sy * o.ny + o.t + 0.5, "L", o.l + 0.5, o.t + 0.5];
		for (var i = 1; i < o.ny; i++) {
			pathArr = pathArr.concat(["M", o.l + 0.5, o.sy * i + o.t + 0.5, "H", o.sx * o.nx + o.l + 0.5]);
		}
		for (var i = 1; i < o.nx; i++) {
			pathArr = pathArr.concat(["M", o.sx * i + o.l + 0.5, o.t + 0.5, "V", o.sy * o.ny + o.t + 0.5]);
		}
		paper.path(pathArr.join(","));
		for (var i = 0; i < o.ny; i++) {
			paper.text(o.l / 2, o.sy * i + o.t, o.ny - i);
		}
		for (var i = 1; i <= o.nx; i++) {
			paper.text(o.sx * i + o.l, o.sy * o.ny + o.t + o.b / 2, i);
		}
		paper.text(o.l / 2, o.sy * o.ny + o.t + o.b / 2, 0);
		if (o.ps) {
			var linePathArr = [];
			for(var i = 0; i < o.ps.length; i++) {
				paper.circle(o.sx * o.ps[i][0] + o.l + 0.5, o.sy * (o.ny - o.ps[i][1]) + o.t + 0.5, o.cr).attr({"fill": "#F00", "stroke": "#F00"}).hover(
					function() {
						var cx = this.attr("cx"), cy = this.attr("cy");
						var x = cx + 10, y = cy + 10, w = 40, h = 20;
						var tmpPath = ["M", x, y, "L", x + w, y, "L", x + w, y + h, "L", x, y + h, "L", x, y];
						self.titleFrame = paper.path(tmpPath.join(",")).attr({"fill": "#000", "stroke": "#666", "stroke-width": 2, "fill-opacity": 0.9});
						var txt = "(" + ((cx - o.l - 0.5) / o.sx) + "," + (o.ny - (cy - o.t - 0.5) / o.sy) + ")";
						self.titleText = paper.text(x + w / 2, y + h / 2, txt).attr({"fill": "#FFF", "font-size": "13px"});;
					},
					function() {
						if (self.titleFrame) self.titleFrame.remove();
						if (self.titleText)	self.titleText.remove();
					}
				);
				linePathArr = linePathArr.concat([i == 0 ? "M" : "L", o.sx * o.ps[i][0] + o.l + 0.5, o.sy * (o.ny - o.ps[i][1]) + o.t + 0.5]);
			}
			paper.path(linePathArr.join(",")).attr({"stroke": "#F00", "stroke-width": o.lw});
		}
		return paper;
	};
	
	$.fn.chart.ops = {
		t: 20, r: 20, b: 30, l: 30,
		sx: 30, sy: 30, nx: 15, ny: 10,
		cr: 5, lw: 1
	};
})(jQuery);